﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Step4.Unit4.AutoMapModels;
using Step4.Unit4.Models;

namespace Step4.Unit4.Controllers
{
    public class TestAutoMapperController  // 在Conrollers文件夹下，只要类型和文件名都以Controller结尾，默认就是控制器(约定的)
    {
        private readonly IMapper _mapper;

        public TestAutoMapperController(IMapper mapper)
        {
            _mapper = mapper;
        }

        /// <summary>
        /// 测试 "不同命名公约间映射"(下划线、小驼峰、大坨峰、前缀、后缀)
        /// </summary>
        public void Test_1_2()
        {
            var product = new Product_1_2()
            {
                categoryName = "1",
                productName = "2"
            };

            var productTo12 = _mapper.Map<Product_1_2, Product_to_1_2>(product);

            Console.WriteLine(productTo12);
        }


        /// <summary>
        /// 测试 "集合映射"
        /// </summary>
        public void Test_2_0()
        {
            List<Product_1_2> list = new List<Product_1_2>()
            {
                    new Product_1_2()
                    {
                        categoryName = "1",
                        productName = "1"
                    },
                    new Product_1_2()
                    {
                        categoryName = "2",
                        productName = "2"
                    },
                    new Product_1_2()
                    {
                        categoryName = "3",
                        productName = "3"
                    }
            };

            var listProdctTo12= _mapper.Map<List<Product_1_2>, List<Product_to_1_2>>(list);
            Console.WriteLine(listProdctTo12);

        }


        /// <summary>
        ///  不规则映射  (名称无归路、名称不同) 需要手动配置映射
        /// </summary>
        public void Test_2_1()
        { 
            var userInfo_2_1 = new UserInfo2_1() {Id = 1,Sex='男',NickName= "赵宁宁" };
            var userInfoTo_2_1 = _mapper.Map<UserInfo2_1, UserInfoTo_2_1>(userInfo_2_1);
            Console.WriteLine(userInfoTo_2_1);
        }


        /// <summary>
        /// 嵌套类(导航属性映射)     
        /// </summary>
        public void Test_2_2()
        {
            var userInfo2_1 = new UserInfo2_1() { Id = 1, NickName = "赵一阳", Sex = '男' };
            var product2_2 = new Product2_2() { Id=1,User= userInfo2_1 };
            var productTo2_2 = _mapper.Map<Product2_2, ProductTo2_2>(product2_2);
            Console.WriteLine(productTo2_2);
        }

        public void Test_2_3()
        {
            ParentSource source = new ChildSource() { Id = 1, Money = 8, Name = "子类实例化父类" };
            var dest= _mapper.Map<ParentSource, ParentTo>(source);
            Console.WriteLine(dest);
        }
    }
}
